package com.wzdigit.wms.wms.controller.basic;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.CreateSkuRequest;
import com.wzdigit.wms.basic.client.request.basic.SearchSkuInputRequest;
import com.wzdigit.wms.basic.client.request.basic.SearchSkuRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateSkuRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateStatusSkuRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.SkuBaseResponse;
import com.wzdigit.wms.basic.client.response.basic.SkuInputResponse;
import com.wzdigit.wms.basic.client.response.basic.SkuResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.basic.SkuService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 货品 前端控制器
 * @author yangdongjie
 * @SkuController.java
 * @2020年12月31日下午5:56:18
 */
@RestController
@Api(tags = "货品")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class SkuController {

	@Autowired
    private SkuService skuService;

    private static final String REQUEST_MODEL = "/sku";
    
    
    /**
     * 查询货品列表
     * @param <T>
     * @param dto
     * @param servletRequest
     * @param servletResponse
     * @return
     */
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "查询货品列表",notes = "查询货品列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchSku")
    public PagerInfo<List<SkuBaseResponse>> searchSku(
    		SearchSkuRequest dto, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return skuService.searchSku(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	
    /**
     * 查询货品
     * @param skuId
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "查询货品",notes = "查询货品")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/getSku")
    public SkuResponse getSku(
    		@ApiParam(value = "货品ID", required = true) 
    		@RequestParam Integer skuId, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return skuService.getSku(skuId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 创建货品
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "创建货品",notes = "创建货品")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/createSku")
    public SkuResponse createSku(
    		@RequestBody @Valid CreateSkuRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return skuService.createSku(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改货品
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改货品",notes = "修改货品")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateSku")
    public SkuResponse updateSku(
    		@RequestBody @Valid UpdateSkuRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return skuService.updateSku(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改货品状态
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改货品状态",notes = "修改货品状态")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateStatus")
    public <T> Result<T> updateStatus(
    		@RequestBody @Valid UpdateStatusSkuRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return skuService.updateStatus(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "查询货物编码(分页)", notes = "查询货物编码(分页)")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/SearchSkuInput", method = RequestMethod.GET)
    public PagerInfo<List<SkuInputResponse>>  searchGoodsSnReport(
    		SearchSkuInputRequest search, Pager<?> pager,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return skuService.getSkuInput(search, pager);
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }
}
